﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Interactivity;

namespace TestColorSwatchSL
{
    public class ListBoxItemSendToTop : TriggerAction<DependencyObject>
    {
        static int ZIndexListBox = 1;

        protected override void OnAttached()
        {
            base.OnAttached();
            FrameworkElement e1 = this.AssociatedObject as FrameworkElement;
            e1.MouseEnter += new MouseEventHandler(e1_MouseEnter);
            e1.MouseLeave += new MouseEventHandler(e1_MouseLeave);
        }

        void e1_MouseLeave(object sender, MouseEventArgs e)
        {
            this.changeZIndex(0);
        }

        void e1_MouseEnter(object sender, MouseEventArgs e)
        {
            this.changeZIndex(++ZIndexListBox);
        }

        protected override void Invoke(object parameter)
        {
           
        }

        private void changeZIndex(int zIndex)
        {
            DependencyObject parent = this.AssociatedObject;
            while (parent != null)
            {
                if (parent.GetType() == typeof(ListBoxItem))
                {
                    Canvas.SetZIndex((FrameworkElement)parent, zIndex);
                    break;
                }
                parent = VisualTreeHelper.GetParent(parent);
            }
        }

     
    }
}
